package com.cec.result;

import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 *
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {


    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 响应码
     */
    private int code;

    /**
     * 信息
     */
    private String msg;

    /**
     * 总数
     */
    private long total = 0;

    /**
     * 数据
     */
    private T data;


    public Result(boolean success, int code, String msg, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(boolean success, int code, String msg,long total, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.total=total;
        this.data = data;
    }

    /**
     * 快速返回一个成功操作
     *
     * @param msg  msg
     * @param data data
     * @param <T>  数据类型
     * @return 结果
     */
    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(true, ResultCode.SUCCESS.getCode(), msg, data);
    }

    /**
     * 快速返回一个成功操作
     *
     * @param data data
     * @param <T>  数据类型
     * @return 结果
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(true, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }
    public static <T> Result<T> success(T data,long total) {
        return new Result<>(true, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), total, data);
    }
    /**
     * 快速返回一个成功操作
     *
     * @return 结果
     */
    public static Result success() {
        return new Result<>(true, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
    }

    /**
     * 快速返回一个成功操作
     *
     * @param msg msg
     * @return 结果
     */
    public static Result success(String msg) {
        return new Result<>(true, ResultCode.SUCCESS.getCode(), msg, null);
    }

    public static <T> Result<T> error(int code, String msg, T data) {
        return new Result<>(false, code, msg, data);
    }

    public static Result error(int code, String msg) {
        return new Result<>(false, code, msg, null);
    }

    public static Result<Void> error(ResultCode code) {
        return new Result<>(false, code.getCode(), code.getMsg(), null);
    }

}
